package com.ajx.blog.controller;

import java.util.HashMap;
import java.util.Map;

import com.ajx.blog.entity.Blogusers;
import com.ajx.blog.service.api.BlogusersService;
import com.ajx.blog.service.api.UserService;

import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/register")
public class RegisterController {

    // 日志
    private Logger logger = LoggerFactory.getLogger(RegisterController.class);

    @Reference(url = "dubbo://localhost:20881", timeout = 2000)
    private BlogusersService blogusersService;

    /**
     * @param {Blogusers} blogusers 待注册的博客用户对象
     * @return {Object}
     * 
     * @description: 博客用户注册
     */
    @PostMapping
    public Object register(@RequestBody Blogusers blogusers) {
        Map<String, Object> returnMap = new HashMap<>();
        // 添加默认头像
        blogusers.setHeadimg("upload/default.jpg");
        // 设置账号状态为正常状态(1)
        blogusers.setUstatus("1");
        if (blogusersService.insertOneUser(blogusers)) {
            returnMap.put("code", 0);
            returnMap.put("msg", "用户注册成功");
            logger.info("用户注册成功");
        } else {
            returnMap.put("code", -1);
            returnMap.put("msg", "用户注册失败");
            logger.info("用户注册失败");
        }
        return returnMap;
    }

}
